using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

/*
 * 人员定位
 * 定时发送post消息
 * 接收后解析
 */
public class PostManager : MonoBehaviour
{
    public static PostManager instance;

    public static PostManager GetIns() 
    {
        if (instance == null) 
        {
            instance = new PostManager();
        }
        return instance;
    }

    public string url = "";
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// POST
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="handerStr">头键</param>
    /// <param name="value">头值</param>
    /// <param name="jsondata">请求包</param>
    /// <param name="postEnum">post内容</param>
    /// <returns></returns>
    public static IEnumerator PostData(string url,string handerStr,string value ,string jsondata,Action<object> valueSetAction)
    {
        byte[] databyte = Encoding.UTF8.GetBytes(jsondata);
        UnityWebRequest _request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
        _request.timeout = 5;//时间限制
        _request.uploadHandler = new UploadHandlerRaw(databyte);//设置包体
        _request.downloadHandler = new DownloadHandlerBuffer();
        _request.SetRequestHeader(handerStr, value);//设置请求头
        _request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
        yield return _request.SendWebRequest();

        //返回服务器响应（如200，404等）
        Debug.Log("请求结果 "+ _request.responseCode);

        if (_request.result == UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError(_request.error);
        }
        else
        {
            Debug.Log(_request.downloadHandler.text);

            #region
            //switch (postEnum)
            //{
            //    case PostEnum.GetToke:
            //        StaffMain.getTokeReply = JsonConvert.DeserializeObject<GetTokeReply>(_request.downloadHandler.text);//获取令牌应答包
            //        break;
            //    case PostEnum.BuildInsiders:
            //        StaffMain.buildInsidersReply = JsonConvert.DeserializeObject<BuildInsidersReply>(_request.downloadHandler.text);//获取建筑物下所有内部人员应答包
            //        break;
            //    case PostEnum.OutSideInsiders:
            //        StaffMain.outSideInsidersReply = JsonConvert.DeserializeObject<OutSideInsidersReply>(_request.downloadHandler.text);
            //        break;
            //}
            #endregion

            valueSetAction(JsonConvert.DeserializeObject(_request.downloadHandler.text));

            //删除并清空downloadHandler占用的资源
            _request.downloadHandler.Dispose();
        }
    }

    public static void enumerator(Action<object> action) 
    {
        BuildInsidersRequest buildInsidersReply = new BuildInsidersRequest() 
        {
            buildId = "110",
            pageNumber = 1,
            pageSize = 15
        };
        action(buildInsidersReply);
    }
}

/// <summary>
/// 获取令牌，获取建筑物下所有内部人员，获取外来人员流水
/// </summary>
public enum PostEnum
{
    GetToke,BuildInsiders, OutSideInsiders
}

